package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.ContractEnterpriseBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.ContractEnterpriseMapper;
import com.molichuxing.services.infrastructure.dto.request.create.ContractEnterpriseCreateDto;
import com.molichuxing.services.infrastructure.dto.response.ContractEnterpriseDto;
import com.molichuxing.services.infrastructure.service.ContractEnterpriseService;
import com.molichuxing.services.property.ContractEnterpriseTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同表 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since
 */
@Service("contractEnterpriseService")
public class ContractEnterpriseServiceImpl implements ContractEnterpriseService {
    private static final Logger logger = LoggerFactory.getLogger(ContractEnterpriseServiceImpl.class);

    @Autowired
    ContractEnterpriseMapper contractEnterpriseMapper;


    /**
     * 添加
     *
     * @param contractEnterpriseCreateDto
     * @return Integer 合同id
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Integer createContract(ContractEnterpriseCreateDto contractEnterpriseCreateDto) throws Exception {
        if (null == contractEnterpriseCreateDto) {
            throw new BizException("合同内容不能为空");
        }

        String contractNo = contractEnterpriseCreateDto.getContractNo();
        if (contractEnterpriseCreateDto.getContractType() == ContractEnterpriseTypeEnum.ORDER_CONTRACT.getValue()) {
            // 根据合同号查询合同信息
            ContractEnterpriseBean enterpriseBean = contractEnterpriseMapper.getByContractNo(contractNo,
                    ContractEnterpriseTypeEnum.ORDER_CONTRACT.getValue());
            if (enterpriseBean != null) {
                if (enterpriseBean.getOrderCode() != null) {
                    // 存在合同号且存在订单号
                    throw new BizException("合同编号" + contractNo + "已存在，请输入正确合同编号");
                } else {
                    // 存合同号，不存在订单号，修改
                    ContractEnterpriseBean modifyBean = Convert.toContractEnterpriseBean(contractEnterpriseCreateDto);
                    modifyBean.setId(enterpriseBean.getId());
                    if (contractEnterpriseMapper.modifyOrderContract(enterpriseBean) > 0) {
                        return enterpriseBean.getId();
                    } else {
                        logger.error("新增修改订单合同失败:", JSONObject.toJSONString(contractEnterpriseCreateDto));
                        throw new BizException("新增订单合同失败!");
                    }
                }
            }
        }

        ContractEnterpriseBean contractEnterpriseBean = Convert.toContractEnterpriseBean(contractEnterpriseCreateDto);
        contractEnterpriseMapper.create(contractEnterpriseBean);
        return contractEnterpriseBean.getId();
    }

    /**
     * 添加订单号
     *
     * @param contractId 合同id
     * @param orderCode  订单号
     * @return
     */
    @Override
    public boolean modifyOrderCode(Integer contractId, Long orderCode) {
        return contractEnterpriseMapper.modifyOrderCode(contractId, orderCode) > 0;
    }

    /**
     * 通过订单号，车辆id获取信息
     *
     * @param orderCode
     * @param carId
     * @return
     */
    @Override
    public ContractEnterpriseDto getContractByOrderCode(Long orderCode, Integer carId, ContractEnterpriseTypeEnum contractEnterpriseTypeEnum) {
        ContractEnterpriseBean contractEnterpriseBean = contractEnterpriseMapper.getContractByOrderCode(orderCode, carId, contractEnterpriseTypeEnum.getValue());
        return Convert.toContractEnterpriseDto(contractEnterpriseBean);
    }

    /**
     * 通过订单号，车辆id获取信息
     *
     * @param orderCode
     * @param carId
     * @param contractEnterpriseTypeEnum
     * @return
     */
    @Override
    public List<ContractEnterpriseDto> getContractListByOrderCode(Long orderCode, Integer carId, ContractEnterpriseTypeEnum contractEnterpriseTypeEnum) {
        List<ContractEnterpriseBean> list = contractEnterpriseMapper.getContractListByOrderCode(orderCode, carId, contractEnterpriseTypeEnum.getValue());
        return Convert.toContractEnterpriseDtoList(list);
    }

    @Override
    public Boolean getContractNoExist(String contractNo) {
        if (StringUtils.isBlank(contractNo)) {
            throw new BizException("合同编号不能为空");
        }

        int count = contractEnterpriseMapper.getContractNoExist(contractNo);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Map<Long, ContractEnterpriseDto> getContractMapByOrderCodes(List<Long> orderCodes) {
        Map<Long, ContractEnterpriseDto> map = new HashMap<>();
        List<ContractEnterpriseBean> list = contractEnterpriseMapper.getContractMapByOrderCodes(orderCodes);
        if (list != null && list.size() > 0) {
            for (ContractEnterpriseBean contractEnterpriseBean : list) {
                ContractEnterpriseDto dto = Convert.toContractEnterpriseDto(contractEnterpriseBean);
                map.put(contractEnterpriseBean.getOrderCode(), dto);
            }
        }
        return map;
    }


}
